package com.astronomy.article.controller;

import com.astronomy.article.pojo.Channel;
import com.astronomy.article.service.ChannelService;
import com.astronomy.entity.PageResult;
import com.astronomy.entity.Result;
import com.astronomy.entity.StatusCode;
import com.github.pagehelper.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/channel")
@CrossOrigin
public class ChannelController {


    @Autowired
    private ChannelService channelService;

    @GetMapping
    public Result findAll(){
        List<Channel> channelList = channelService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",channelList) ;
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        Channel channel = channelService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",channel);
    }

    @PostMapping
    public Result add(@RequestBody Channel channel){
        channelService.add(channel);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @PutMapping(value="/{id}")
    public Result update(@RequestBody Channel channel, @PathVariable String id){
        channel.setId(id);
        channelService.update(channel);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        channelService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        Page<Channel> pageList = channelService.findPage(page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    @PostMapping(value = "/search" )
    public Result findList(@RequestBody Map searchMap){
        List<Channel> list = channelService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody Map searchMap, @PathVariable  int page, @PathVariable  int size){
        PageResult pageResult = channelService.findPage(searchMap, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

}
